//
//  LBTool.m
//  Holyiot-DFU
//
//  Created by bill on 2021/4/13.
//

#import "LBTool.h"

@implementation LBTool
#pragma mark =================== 字符串转bytes ======================
//十六进制字符串转 data   "fa4589"-->>0xfa4589
+(NSData*) convertBytesStringToData:(NSString*)hexString leng:(NSInteger)leng fillPosition:(FillPosition)position
{
    NSMutableData* data = [NSMutableData data];
    //如果是奇位数,补齐一个字节(补零)
    NSString *newStr = hexString;
//    if (hexString.length%2) {
//        if (position == FillPositionFront) {
//            newStr = [@"0" stringByAppendingString:hexString];
//        }else{
//            newStr = [hexString stringByAppendingString:@"0"];
//        }
//
//    }else{
//        newStr = hexString;
//    }
    //不足字节数在前面补齐0
    if (newStr.length<leng*2 && position==FillPositionFront) {
        NSMutableString *mtbStr = [NSMutableString new];
        for (int i=0; i<leng*2-newStr.length; i++) {
//            Byte zero[] = {0x00};
//            NSData *zeroData = [[NSData alloc]initWithBytes:zero length:sizeof(zero)];
//            [data appendData:zeroData];
           
            [mtbStr appendString:@"0"];
            NSLog(@"---->%d  newStr:%@",i,mtbStr);
        }
        [mtbStr appendString:hexString];
        newStr = mtbStr.copy;
    }
    //不足字节数在后面补齐0
    if (newStr.length<leng*2 && position==FillPositionBehind) {
        NSMutableString *mtbStr = newStr.mutableCopy;
        for (int i=0; i<leng*2-newStr.length; i++) {
//            Byte zero[] = {0x00};
//            NSData *zeroData = [[NSData alloc]initWithBytes:zero length:sizeof(zero)];
//            [data appendData:zeroData];
           
            [mtbStr appendString:@"0"];
            NSLog(@"---->%d  newStr:%@",i,mtbStr);
        }
        newStr = mtbStr.copy;
    }
    
    //多于指定字节,不管是前面补0还是后面补0,都去除多余的
    if ((hexString.length>leng*2) && (leng!=0)) {
//        NSString *str = newStr.copy;
        newStr = [hexString substringWithRange:NSMakeRange(0, leng*2)];
    }
    int idx;
    //转成data
    for (idx = 0; idx+2 <= newStr.length; idx+=2) {
        NSRange range = NSMakeRange(idx, 2);
        NSString* hexStr = [newStr substringWithRange:range];
        NSScanner* scanner = [NSScanner scannerWithString:hexStr];
        unsigned int intValue;
        [scanner scanHexInt:&intValue];
        [data appendBytes:&intValue length:1];
    }
    
    
    return data;
}
//十进制字符串转换为十六进制字符串  "12345"--->"0x3039"
+ (NSString *)convertDecimalismStrToHexStr:(NSString *)string
{
    int tmpid = [string intValue];
    NSString *nLetterValue;

    NSString *str =@"";

    long long int ttmpig;

    for (int i =0; i<9; i++) {

        ttmpig=tmpid%16;

        tmpid=tmpid/16;

        switch (ttmpig)

        {

            case 10:

                nLetterValue =@"A";break;

            case 11:

                nLetterValue =@"B";break;

            case 12:

                nLetterValue =@"C";break;

            case 13:

                nLetterValue =@"D";break;

            case 14:

                nLetterValue =@"E";break;

            case 15:

                nLetterValue =@"F";break;

            default:nLetterValue=[[NSString alloc]initWithFormat:@"%lli",ttmpig];
          

        }
        str = [nLetterValue stringByAppendingString:str];
        if (tmpid == 0) {
            break;
        }
    }

    return str;
    
}
#pragma mark ------- 有符号16进制转 10进制 如:"0xed"-->"-19" -------
+ (int)convertSignedHexStrToInt:(NSString *)string{
    char *_0x16String = (char *)string.UTF8String;
    NSMutableString *binaryString = [[NSMutableString alloc] init];
    for (int i = 0; i < string.length; i++) {
        char c = _0x16String[i];
        NSString *binary = [self convertHexStrToDecimalismStr:[NSString stringWithFormat:@"%c",c]];
        [binaryString appendString:binary];
    }
    
    if ([binaryString characterAtIndex:0] == '1') {
        //反码
        for (int i = (int)binaryString.length - 1; i > 0; i--) {
            char c = [binaryString characterAtIndex:i];
            c = c^0x1;
            [binaryString replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",c]];
        }
        
        //补码
        BOOL flag = NO; //进位
        NSInteger lastIndex = binaryString.length - 1;
        char lastChar = [binaryString characterAtIndex:lastIndex];
        if (lastChar == '0') {
            lastChar = '1';
        } else {
            lastChar = '0';
            flag = YES;
        }
        
        [binaryString replaceCharactersInRange:NSMakeRange(lastIndex, 1) withString:[NSString stringWithFormat:@"%c",lastChar]];
        
        if (flag) {
            for (int i = (int)binaryString.length - 2; i > 0; i--) {
                char c = [binaryString characterAtIndex:i];
                if (flag) {//进位
                    if (c == '0') {
                        c = '1';
                        flag = NO;
                        [binaryString replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",c]];
                        break;
                    } else if (c == '1'){
                        c = '0';
                        flag = YES;
                        [binaryString replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",c]];
                    }
                }
            }
        }
    }
    
    int result = 0;
    int bit = 0;
    //计算
    for (int i = (int)binaryString.length - 1; i > 0; i--) {
        char c = [binaryString characterAtIndex:i];
        if (c == '1') {
            result += pow(2, bit);
        }
        ++bit;
    }
    if ([binaryString characterAtIndex:0] == '1') {
        result = result *-1;
    }
    return result;
}
#pragma mark -----  现将16进制转换车无符号的10进制
+ (NSString *)convertHexStrToDecimalismStr:(NSString *)string
{
    // 现将16进制转换车无符号的10进制
    long a = strtoul(string.UTF8String, NULL, 16);
    NSMutableString *binary = [[NSMutableString alloc] init];
    while (a/2 !=0) {
        [binary insertString:[NSString stringWithFormat:@"%ld",a%2] atIndex:0];
        a = a/2;
    }
    
    [binary insertString:[NSString stringWithFormat:@"%ld",a%2] atIndex:0];
    
    //不够4位的高位补0
    while (binary.length%4 !=0) {
        [binary insertString:@"0" atIndex:0];
    }
    return binary;
}

+ (long)convertHexStrToDecimalism:(NSString *)string
{
    // 现将16进制转换成无符号的10进制
    long a = strtoul(string.UTF8String, NULL, 16);
    return  a;
}

//十六进制data转成十进制 如:0xde3445-->14562373

+(int)convertHexDataToDecimalism:(NSData*)hexData
{
//    NSLog(@"data=%@",hexData);
    Byte *temp_byte = (Byte*)[hexData bytes];
    
    int temp = 0;
    //高位在前 4c b9 = 19641
    for (int i = hexData.length-1; i>=0; i--) {
        temp = temp|((temp_byte[i]&0xff)<<(8*(hexData.length-1-i)));
//        NSLog(@"byte[%d]=%x",i,temp_byte[i]);
    }
    return temp;
    //高位在后 4c b9 = 47436
//    int i;
//    [data getBytes:&i length:sizeof(i)];
//    return i;
}
//十六进制bytes转成十六进制字符串 如:0xde3445-->@"de3445"
+(NSString*)convertHexDataToHexString:(NSData*)hexData
{
    Byte *beacon_byte= (Byte*)[hexData bytes];
    NSMutableString *hexMtbString = [NSMutableString new];
    for (int i=0; i<hexData.length; i++) {
        //转成16进制字符串
        NSString *temp = [NSString stringWithFormat:@"%02x",beacon_byte[i]];
        [hexMtbString appendString:temp];
    }
    return hexMtbString.copy;
}

+(NSString*)getEddyStone_URL_prefix:(NSData*)data
{
    Byte *URL_type_byte = (Byte*)[data bytes];
    NSString *url_preStr = @"";
    if (URL_type_byte[0] == 0x00) {
        url_preStr = @"http://www.";
    }
    if (URL_type_byte[0] == 0x01) {
        url_preStr = @"https://www.";
    }
    if (URL_type_byte[0] == 0x02) {
        url_preStr = @"http://";
    }
    if (URL_type_byte[0] == 0x03) {
        url_preStr = @"https://";
    }
    return url_preStr;
}
+(NSString*)getEddyStone_URL_subfix:(NSData*)data
{
    Byte *URL_type_byte = (Byte*)[data bytes];
    switch (URL_type_byte[0]) {
      case 0x00:
        return @".com/";
      case 0x01:
        return @".org/";
      case 0x02:
        return @".edu/";
      case 0x03:
        return @".net/";
      case 0x04:
        return @".info/";
      case 0x05:
        return @".biz/";
      case 0x06:
        return @".gov/";
      case 0x07:
        return @".com";
      case 0x08:
        return @".org";
      case 0x09:
        return @".edu";
      case 0x0a:
        return @".net";
      case 0x0b:
        return @".info";
      case 0x0c:
        return @".biz";
      case 0x0d:
        return @".gov";
      default:
        return @"";
    }
}
@end
